package com.arlen.centre.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.arlen.centre.service.WxUserService;
import com.arlen.common.constant.GetHttpConstant;
import com.arlen.common.dto.WxUserListDTO;
import com.arlen.common.dto.WxUserMesDTO;
import com.arlen.common.po.WxUserMesPO;
import com.arlen.until.date.DateTimeUntil;
import com.arlen.until.http.Interaction;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

@Service
//@Scope("prototype")
public class WxUserServiceImpl implements WxUserService {

    private static String listUserUrl ="https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";

    @Autowired
    Interaction interaction;


    //获取用户openId列表
    @Override
    public <T> T listUser(@NotNull String token, String nextOpenid) {
        if (StringUtils.isBlank(nextOpenid)){
            nextOpenid ="";
        }
        listUserUrl = listUserUrl.replace("ACCESS_TOKEN",token).replace("NEXT_OPENID",nextOpenid);
        String listUserMes =null;
        try {
            listUserMes = interaction.sendGet(listUserUrl, GetHttpConstant.NOANALYSIS.getState());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (StringUtils.isNoneBlank(listUserMes)){

            return (T) JSON.parseObject(listUserMes, WxUserListDTO.class);

        }
        return null;
    }

    private String getUserMesUrl ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    //单独获取用户列表
    @Override
    public <T> T getUserMes(@NotNull String token, @com.sun.istack.internal.NotNull String openid, String lang) {
        getUserMesUrl = getUserMesUrl.replace("ACCESS_TOKEN",token).replace("OPENID",openid);
        WxUserMesPO wxUserMesPO = new WxUserMesPO();
        if (StringUtils.isNoneBlank(lang)){
            getUserMesUrl = getUserMesUrl.replace("zh_CN",lang);
        }
        try {
            String userMes = interaction.sendGet(getUserMesUrl, GetHttpConstant.NOANALYSIS.getState());
            ObjectMapper objectMapper = new ObjectMapper();
            WxUserMesDTO userMesDTO = objectMapper.readValue(userMes, WxUserMesDTO.class);
            BeanUtils.copyProperties(userMesDTO,wxUserMesPO);
            if (userMesDTO.getSubscribe_time()!=null){
                wxUserMesPO.setSubscribe_time(DateTimeUntil.getDateTimeOfTimestamp(userMesDTO.getSubscribe_time()));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //string 是在堆中存放
            this.getUserMesUrl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        }

        return (T)wxUserMesPO;
    }
}
